package com.gujiangbo.threadpool;

/**
 * 该类是Runnable的实现类，主要用于线程池内部。该类会使用
 * 到RunnableQueue,然后不断的从queue中取出某个runnable.
 * 并运行run方法
 */
public class InternalTask implements Runnable {

    private final RunnableQueue runnableQueue;

    private volatile boolean running = true;

    public InternalTask(RunnableQueue runnableQueue) {
        this.runnableQueue = runnableQueue;
    }

    @Override
    public void run() {

        while (running && !Thread.currentThread().isInterrupted()) {
            try {
                Runnable task = runnableQueue.take();
                task.run();
            } catch (Exception e) {
                running = false;
                break;
            }
        }
    }

    /**
     * 停止任务
     */
    public void stop() {
        this.running = false;
    }
}
